package com.asimplefox.controllers;

import com.asimplefox.entity.Types;
import com.asimplefox.entity.vo.VidListVo;
import com.asimplefox.service.ITypesService;
import com.asimplefox.service.IVidListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 1.添加视频列表接口：添加视频名称，视频封面，视频简介，添加视频分类状态，添加视频时长
 * <br>2.修改视频列表接口：修改视频名称，视频封面，视频简介，修改视频分类状态，修改视频时长
 * <br>3.删除视频列表接口：删除对应的视频列表
 * <br>4.查询视频列表接口：查询对应的视频列表信息
 *
 * <br>GET /api/videos: 获取视频列表。
 * <br>POST /api/videos: 添加新视频，需提供视频名称、封面、简介、所属电视剧ID、剧集数和分类。
 * <br>PUT /api/videos/:id: 更新指定ID的视频信息，需提供新名称、封面和简介。
 * <br>DELETE /api/videos/:id: 删除指定ID的视频。
 *
 * @author github.kloping
 */
@RestController
@RequestMapping("/api/videos")
@Slf4j
public class VidListController {

    @Autowired
    IVidListService vidListService;

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping
    public Object post(@RequestParam("name") String name, @RequestParam("icon") MultipartFile mf,
                       @RequestParam("desc") String desc, @RequestParam("type") String type,
                       @RequestParam("ftype") Integer ftype, @RequestParam("types") @Nullable List<String> types) {
        VidListVo vo = new VidListVo(name, mf, desc, type, ftype);
        return vidListService.save(vo, types);
    }

    @PreAuthorize("hasAnyAuthority('admin','system')")
    @PutMapping
    public Object put(@RequestParam("id") Integer id, @RequestParam("name") String name,
                      @RequestParam("icon") @Nullable MultipartFile mf, @RequestParam("desc") String desc,
                      @RequestParam("type") String type, @RequestParam("ftype") @Nullable Integer ftype
            , @RequestParam("types") @Nullable List<String> types) {
        VidListVo vo = new VidListVo(id, name, mf, desc, type, ftype);
        return vidListService.update(vo,types);
    }

    @GetMapping
    public Object listGet(@RequestParam("id") @Nullable Integer id, @RequestParam("type") @Nullable String type,
                          @RequestParam(value = "ftype", defaultValue = "0") @Nullable Integer ftype,
                          @RequestParam(value = "types") @Nullable String stype,
                          @RequestParam(value = "ps", defaultValue = "5") Integer pageSize,
                          @RequestParam(value = "pn", defaultValue = "1") Integer pageNum) {
        return vidListService.filterList(id, type, ftype, stype, pageSize, pageNum);
    }

    @PreAuthorize("hasAnyAuthority('admin','system')")
    @DeleteMapping
    public Object delete(@RequestParam("id") Integer id) {
        return vidListService.removeById(id);
    }

    @GetMapping("/search")
    public Object search(@RequestParam("keyword") String keyword,
                         @RequestParam(value = "ps", defaultValue = "10") Integer pageSize,
                         @RequestParam(value = "pn", defaultValue = "1") Integer pageNum) {
        return vidListService.search(keyword);
    }

    @GetMapping("/random")
    public Object random(@RequestParam(value = "count" ,defaultValue = "10") Integer count) {
        return vidListService.random(count);
    }


    @RequestMapping("/weekone")
    public Object weekone() {
        return vidListService.weekone();
    }
}
